package cn.feiyang.littletool;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.InputStream;
import java.net.URL;

public class BatchChangeFileName extends JFrame {

    public static final Integer WINDOW_WIDTH = 800;
    public static final Integer WINDOW_HEIGHT = 450;
    public static final Integer MARGIN = 10;

    public static final Integer LEFT_WIDTH = 450;
    public static final Integer RIGHT_WIDTH = 350;

    public static final Integer LINE_HEIGHT = 50;
    public static final Integer LABEL_WIDTH = 100;
    public static final Integer LABEL_HEIGHT = LINE_HEIGHT - MARGIN * 2;
    public static final Integer TEXT_FIELD_WIDTH = 200;

    public static final Font font = new Font("宋体", Font.PLAIN, 16);

    private File[] files;
    private String filePath;

    private JPanel leftPanel;//左侧面板
    private JPanel rightPanel;//右侧面板

    private JLabel filePathLabel;//文件路径选择标签
    private JTextField filePathTextField;//文件路径文本框
    private JButton chooseButton;//文件路径选择按钮
    private JFileChooser fileChooser;//文件选择器

    private JScrollPane previewPanel;//预览面板
    private JTextArea textArea;//预览文件名

    private JPanel fileNamePanel;//右侧文件名面板
    private JLabel fileNameLabel;//右侧文件名标签
    private JTextField fileNameTextField;//右侧文件名文本框

    private JPanel fileNumberPanel;//右侧文件名编号面板
    private JLabel fileNumberLabel;//右侧文件名编号标签
    private JTextField fileNumberTextField;//右侧文件名编号文本框

    private JPanel fileTypePanel;//右侧文件名后缀面板
    private JLabel fileTypeLabel;//右侧文件名后缀标签
    private JTextField fileTypeTextField;//右侧文件名后缀文本框

    private JButton previewButton;//预览按钮
    private JButton executeButton;//执行按钮

    public static Color backgroundColor = new Color(212, 212, 212);


    public BatchChangeFileName() {

        URL url = this.getClass().getResource("/resources/icon2.png");
        this.setIconImage(new ImageIcon(url).getImage());
        this.setLayout(null);
        this.setSize(WINDOW_WIDTH + 50, WINDOW_HEIGHT + 50);
        this.setTitle("批量修改文件名");

        //主面板
        Container contentPane = this.getContentPane();
//        contentPane.setBackground(backgroundColor);

        leftPanel = createJPanel(MARGIN, MARGIN, LEFT_WIDTH, WINDOW_HEIGHT);
        contentPane.add(leftPanel);

        rightPanel = createJPanel(MARGIN * 2 + LEFT_WIDTH, MARGIN, RIGHT_WIDTH, WINDOW_HEIGHT);
        contentPane.add(rightPanel);

        fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

        //左侧文件路径区域
        filePathLabel = createJLabel(MARGIN, MARGIN, LEFT_WIDTH - LABEL_WIDTH * 2, LABEL_HEIGHT, "文件夹路径");
        filePathTextField = createJTextField(MARGIN + LABEL_WIDTH, MARGIN, TEXT_FIELD_WIDTH, LABEL_HEIGHT);
        chooseButton = createJButton(MARGIN * 2 + LABEL_WIDTH + TEXT_FIELD_WIDTH, MARGIN, LABEL_WIDTH, LABEL_HEIGHT, "选择");
        leftPanel.add(filePathLabel);
        leftPanel.add(filePathTextField);
        leftPanel.add(chooseButton);

        //左侧预览文件列表面板
        textArea = new JTextArea();
        textArea.setEnabled(false);
        previewPanel = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        previewPanel.setBounds(MARGIN, MARGIN + LINE_HEIGHT, LEFT_WIDTH - MARGIN * 2, WINDOW_HEIGHT - LINE_HEIGHT - MARGIN * 4);
        leftPanel.add(previewPanel);

        //右侧文件名面板
        fileNamePanel = createJPanel(MARGIN, MARGIN, RIGHT_WIDTH - MARGIN * 2, LINE_HEIGHT);
        fileNameLabel = createJLabel(MARGIN, MARGIN, LABEL_WIDTH, LABEL_HEIGHT, "前置文件名");
        fileNameTextField = createJTextField(MARGIN + LABEL_WIDTH, MARGIN, TEXT_FIELD_WIDTH, LABEL_HEIGHT);
        fileNamePanel.add(fileNameLabel);
        fileNamePanel.add(fileNameTextField);
        rightPanel.add(fileNamePanel);

        //右侧编号面板
        fileNumberPanel = createJPanel(MARGIN, MARGIN * 2 + LINE_HEIGHT, RIGHT_WIDTH - MARGIN * 2, LINE_HEIGHT);
        fileNumberLabel = createJLabel(MARGIN, MARGIN, LABEL_WIDTH, LABEL_HEIGHT, "起始编号");
        fileNumberTextField = createJTextField(MARGIN + LABEL_WIDTH, MARGIN, TEXT_FIELD_WIDTH, LABEL_HEIGHT);
        fileNumberPanel.add(fileNumberLabel);
        fileNumberPanel.add(fileNumberTextField);
        rightPanel.add(fileNumberPanel);

        //右侧文件类型面板
        fileTypePanel = createJPanel(MARGIN, MARGIN * 3 + LINE_HEIGHT * 2, RIGHT_WIDTH - MARGIN * 2, LINE_HEIGHT);
        fileTypeLabel = createJLabel(MARGIN, MARGIN, LABEL_WIDTH, LABEL_HEIGHT, "文件名后缀");
        fileTypeTextField = createJTextField(MARGIN + LABEL_WIDTH, MARGIN, TEXT_FIELD_WIDTH, LABEL_HEIGHT);
        fileTypePanel.add(fileTypeLabel);
        fileTypePanel.add(fileTypeTextField);
        rightPanel.add(fileTypePanel);

        //预览按钮
        previewButton = createJButton(MARGIN, MARGIN * 4 + LINE_HEIGHT * 3, LABEL_WIDTH, LABEL_HEIGHT, "预览");
        rightPanel.add(previewButton);
        //执行按钮
        executeButton = createJButton(MARGIN * 2 + LABEL_WIDTH, MARGIN * 4 + LINE_HEIGHT * 3, LABEL_WIDTH, LABEL_HEIGHT, "执行");
        rightPanel.add(executeButton);

        //选择文件路径
        chooseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int result = fileChooser.showDialog(null, "选择");
                if (result == JFileChooser.APPROVE_OPTION) {
                    File directory = fileChooser.getSelectedFile();
                    filePath = directory.getAbsolutePath();
                    filePathTextField.setText(filePath);

                    files = directory.listFiles();
                    textArea.setText("");
                    StringBuffer buf = new StringBuffer();
                    for (int i = 0; i < files.length; i++) {
                        buf.append(files[i].getName()).append("\r\n");
                    }
                    textArea.setText(buf.toString());
                } else {
                    fileChooser.cancelSelection();
                }

            }
        });

        //预览修改结果
        previewButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                textArea.setText("");
                if (validateFileInfo()) {
                    if (files != null && files.length > 0) {
                        String fileName = fileNameTextField.getText();
                        Integer fileNumber = Integer.parseInt(fileNumberTextField.getText());
                        String fileType = fileTypeTextField.getText();

                        StringBuffer buf = new StringBuffer();
                        for (int i = 0; i < files.length; i++) {
                            String newFilename = fileName + (i + fileNumber) + "." + fileType;
                            buf.append(newFilename).append("\r\n");
                        }
                        textArea.setText(buf.toString());
                    } else {
                        JOptionPane.showMessageDialog(null, "文件夹路径为空");
                    }
                }

            }
        });

        //执行修改
        executeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (files != null && files.length > 0) {
                    if (validateFileInfo()) {
                        String fileName = fileNameTextField.getText();
                        Integer fileNumber = Integer.parseInt(fileNumberTextField.getText());
                        String fileType = fileTypeTextField.getText();
                        for (int i = 0; i < files.length; i++) {
                            String filename = fileName + (i + fileNumber) + "." + fileType;
                            File file = files[i];
                            file.renameTo(new File(filePath, filename));
                        }
                        JOptionPane.showMessageDialog(null, "批量修改完毕");
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "文件夹路径为空");
                }
            }
        });

        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setVisible(true);


    }

    public JPanel createJPanel(int x, int y, int width, int height) {
        JPanel jPanel1 = new JPanel(null);
//        jPanel1.setBackground(backgroundColor);
        jPanel1.setBounds(x, y, width, height);
        jPanel1.setBorder(BorderFactory.createTitledBorder(""));
        return jPanel1;
    }

    public JLabel createJLabel(int x, int y, int width, int height, String text) {
        JLabel jLabel = new JLabel(text);
        jLabel.setBounds(x, y, width, height);
        jLabel.setFont(font);
        return jLabel;
    }

    public JTextField createJTextField(int x, int y, int width, int height) {
        JTextField jTextField = new JTextField();
        jTextField.setBounds(x, y, width, height);
        jTextField.setFont(font);
        return jTextField;
    }

    public JButton createJButton(int x, int y, int width, int height, String text) {
        JButton jButton = new JButton();
        jButton.setText(text);
        jButton.setBounds(x, y, width, height);
        jButton.setFont(font);
        return jButton;
    }

    public boolean validateFileInfo() {
        String fileName = fileNameTextField.getText();
        String fileNumberStr = fileNumberTextField.getText();
        String fileType = fileTypeTextField.getText();
        if (fileName == null || fileName.trim().equals("")) {
            JOptionPane.showMessageDialog(null, "请输入文件名");
            return false;
        }
        if (fileNumberStr == null || fileNumberStr.trim().equals("")) {
            JOptionPane.showMessageDialog(null, "请输入起始文件名编号");
            return false;
        }
        try {
            int cc = Integer.parseInt(fileNumberStr);
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "起始编号必须为数字");
            return false;
        }
        if (fileType == null || fileType.trim().equals("")) {
            JOptionPane.showMessageDialog(null, "请输入文件类型或后缀");
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
//        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
//        UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
//        UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
//        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");

        new BatchChangeFileName();

    }
}
